package com.leapp.sk.ui.adapter

import android.content.Context
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMDirect
import com.example.mvp.adapter.BaseLibMultipleViewAdapter
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.delegate.BaseAdapterDelegate
import com.leapp.sk.binder.im.SingleChatAdapterBinder
import com.example.im.server.im.MessageHandleServer
import com.leapp.sk.util.SKTimeUtil
import com.example.util.finallist.IMExtKey
import com.example.util.finallist.SkKey
import com.leapp.sk.viewdelegate.adapter.im.*

/**
 * 单聊适配器
 * @author  chaohaipeng  on 2018/7/17.
 */
class SingleChatAdapter(context: Context, mlist: List<LKIMMessage>) : BaseLibMultipleViewAdapter<LKIMMessage>(context, mlist) {
    /**
     * 表情数据
     */
    val mList = mlist
    /**
     * 上下文
     */
    val mContext = context

    override fun bindEvenListener(viewDelegate: BaseAdapterDelegate?, position: Int) {
    }

    override fun getItemViewType(position: Int): Int {


        val get = mList.get(position)
        val mesgType = get.getAttribute(IMExtKey.EXTKEY_MESSAGETYPE, SkKey.BTKEY_String_1)
        if (TextUtils.equals(mesgType, SkKey.BTKEY_String_0)) {
            return 0
        } else if (TextUtils.equals(mesgType, SkKey.BTKEY_String_1)) {
            return 1
        } else if (TextUtils.equals(mesgType, SkKey.BTKEY_String_2)) {
            return 2
        } else if (TextUtils.equals(mesgType, SkKey.BTKEY_String_3)) {
            return 3
        }  else if (TextUtils.equals(mesgType, SkKey.BTKEY_String_5)) {
            return 5
        } else {
            return 1
        }

    }

    override fun notifyView(viewDelegate: BaseAdapterDelegate?, position: Int) {
        val lsit = mList.get(position)
        MessageHandleServer.getInstance().messageAck(lsit)
        if (viewDelegate is OtherNoticeMsgAdapterDelegate) {
            var timeInterval: Int = 0
            if (position != 0) {
                timeInterval = SKTimeUtil.getTimeInterval(lsit.msgTime, mList.get(position - 1).msgTime)//两次聊天信息之间的时间间隔
            } else {
            }
            viewDelegate.setRecerveView(mContext, lsit, timeInterval, position)
        } else if (viewDelegate is SingleChatAdapterDelegale) {
            var timeInterval: Int = 0
            if (position != 0) {
                timeInterval = SKTimeUtil.getTimeInterval(lsit.msgTime, mList.get(position - 1).msgTime)//两次聊天信息之间的时间间隔
            } else {
            }
            if (lsit.lkimDirect == LKIMDirect.SEND) {
                viewDelegate.setSendDataView(mContext, lsit, timeInterval, position)
            } else if (lsit.lkimDirect == LKIMDirect.RECEIVE) {
                viewDelegate.setRecerveView(mContext, lsit, timeInterval, position)
            }
        } else if (viewDelegate is ChatImageAdapterDelegate) {
            var timeInterval: Int = 0
            if (position != 0) {
                timeInterval = SKTimeUtil.getTimeInterval(lsit.msgTime, mList.get(position - 1).msgTime)//两次聊天信息之间的时间间隔
            } else {
            }
            if (lsit.lkimDirect == LKIMDirect.SEND) {
                viewDelegate.setSendDataView(mContext, lsit, timeInterval, position)
            } else if (lsit.lkimDirect == LKIMDirect.RECEIVE) {
                viewDelegate.setRecerveView(mContext, lsit, timeInterval, position)
            }
        } else if (viewDelegate is ChatVoiceAdapterDelegate) {
            var timeInterval: Int = 0
            if (position != 0) {
                timeInterval = SKTimeUtil.getTimeInterval(lsit.msgTime, mList.get(position - 1).msgTime)//两次聊天信息之间的时间间隔
            } else {
            }
            if (lsit.lkimDirect == LKIMDirect.SEND) {
                viewDelegate.setSendDataView(mContext, lsit, timeInterval, position)
            } else if (lsit.lkimDirect == LKIMDirect.RECEIVE) {
                viewDelegate.setRecerveView(mContext, lsit, timeInterval, position)
            }

        } else if (viewDelegate is ChatVideoAdapterDelegate) {
            var timeInterval: Int = 0
            if (position != 0) {
                timeInterval = SKTimeUtil.getTimeInterval(lsit.msgTime, mList.get(position - 1).msgTime)//两次聊天信息之间的时间间隔
            } else {
            }
            if (lsit.lkimDirect == LKIMDirect.SEND) {
                viewDelegate.setSendDataView(mContext, lsit, timeInterval, position)
            } else if (lsit.lkimDirect == LKIMDirect.RECEIVE) {
                viewDelegate.setRecerveView(mContext, lsit, timeInterval, position)
            }

        }
    }

    override fun getDelegate(position: Int): BaseAdapterDelegate {
        val itemViewType = getItemViewType(position)
        val lsit = mList.get(position)
        val mesgType = lsit.getAttribute(IMExtKey.EXTKEY_MESSAGETYPE, SkKey.BTKEY_String_1)
        when (itemViewType) {
            0 -> {
                return OtherNoticeMsgAdapterDelegate::class.java?.newInstance()
            }
            1 -> {
                return SingleChatAdapterDelegale::class.java?.newInstance()
            }
            2 -> {
                return ChatVoiceAdapterDelegate::class.java?.newInstance()
            }
            3 -> {
                return ChatImageAdapterDelegate::class.java?.newInstance()
            }
            5 -> {
                return ChatVideoAdapterDelegate::class.java?.newInstance()
            }
            else -> {
                return SingleChatAdapterDelegale::class.java?.newInstance()
            }
        }
    }

    override fun getDataBinder(position: Int): BaseDataBinder<*, *> {
        return SingleChatAdapterBinder()

    }


    override fun getItemData(position: Int): Any {
        return mList.get(position)
    }


}